package org.linlinjava.litemall.admin.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.service.AdminActivityListService;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallRole;
import org.linlinjava.litemall.db.domain.vo.LitemallActivityCreateVo;
import org.linlinjava.litemall.db.domain.vo.LitemallActivityGrouponEditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin/activity")
@Validated
public class AdminActivityController {

    @Autowired
    private AdminActivityListService adminActivityListService;

    private LitemallAdmin getUser() {
        Subject currentUser = SecurityUtils.getSubject();
        return (LitemallAdmin) currentUser.getPrincipal();
    }

    private Boolean isActAdmin(LitemallAdmin admin) {
        List<Integer> roleIds = Arrays.asList(admin.getRoleIds());
        return roleIds.contains(LitemallRole.RoleEnum.SUPER.getCode())
                || roleIds.contains(LitemallRole.RoleEnum.ACT_SUPER.getCode());
    }

    @RequiresPermissions("admin:activity:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String title, String startTime, String endTime,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        LitemallAdmin admin = getUser();
        if (isActAdmin(admin)) {
            admin = null;
        }
        return adminActivityListService.activityList(title, startTime, endTime, admin, page, limit, sort, order);
    }


    @RequiresPermissions("admin:activity:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallActivityCreateVo activity) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.createActivity(activity, admin);
    }

    @RequiresPermissions("admin:activity:edit")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "编辑")
    @PostMapping("/edit")
    public Object edit(@RequestBody LitemallActivityCreateVo activity) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.updateActivity(activity, isActAdmin(admin), admin);
    }

    @RequiresPermissions("admin:activity:changeState")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "活动上下架")
    @PostMapping("/changeState")
    public Object changeState(@RequestBody LitemallActivityCreateVo activity) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.changeState(activity, isActAdmin(admin), admin);
    }

    @RequiresPermissions("admin:activity:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.queryActivityDetail(id, isActAdmin(admin), admin);
    }


    @RequiresPermissions("admin:activityGroupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "查询团购信息")
    @GetMapping("/activityGroupon/list")
    public Object activityGrouponList(Byte userType, String activityId, String subject, String grouponCode,
                                      String userName, Byte state,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer limit,
                                      @Sort @RequestParam(defaultValue = "groupon_code") String sort,
                                      @Order @RequestParam(defaultValue = "desc") String order) {
        return adminActivityListService.activityGrouponList(userType, activityId, subject, grouponCode, userName, state, page, limit, sort, order);
    }

    @RequiresPermissions("admin:activityGroupon:exportList")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "导出团购信息")
    @GetMapping("/activityGroupon/exportList")
    public Object activityGrouponListForExport(Byte userType, String activityId, String subject, String grouponCode,
                                      String userName, Byte state) {
        return adminActivityListService.activityGrouponList(userType, activityId, subject, grouponCode, userName, state);
    }

    @RequiresPermissions("admin:activityGroupon:grouponUserList")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "团中的参团团员")
    @PostMapping("/activityGroupon/grouponUserList")
    public Object grouponUserList(@RequestBody LitemallActivityGrouponEditVo vo) {
        return adminActivityListService.grouponUserList(vo);
    }


    @RequiresPermissions("admin:activityGroupon:changeGroupon")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "调去其他团")
    @PostMapping("/activityGroupon/changeGroupon")
    public Object changeGroupon(@RequestBody LitemallActivityGrouponEditVo vo) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.changeGroupon(vo, admin);
    }


    @RequiresPermissions("admin:activityGroupon:listActivityGroupon")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "活动中的团")
    @PostMapping("/activityGroupon/listActivityGroupon")
    public Object listActivityGroupon(@RequestBody LitemallActivityGrouponEditVo vo) {
        return adminActivityListService.listActivityGroupon(vo);
    }


    @RequiresPermissions("admin:activityGroupon:replaceCaptain")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "替换团长")
    @PostMapping("/activityGroupon/replaceCaptain")
    public Object replaceCaptain(@RequestBody LitemallActivityGrouponEditVo vo) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.replaceCaptain(vo, admin);
    }

    @RequiresPermissions("admin:activityGroupon:changeUserType")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "变更新老学员")
    @PostMapping("/activityGroupon/changeUserType")
    public Object changeUserType(@RequestBody LitemallActivityGrouponEditVo vo) {
        LitemallAdmin admin = getUser();
        return adminActivityListService.changeUserType(vo, admin);
    }

    @RequiresPermissions("admin:activityGroupon:independent")
    @RequiresPermissionsDesc(menu = {"推广管理", "活动模板管理"}, button = "独立成团")
    @PostMapping("/activityGroupon/independent")
    public Object independent(@RequestBody LitemallActivityGrouponEditVo vo) {
        return adminActivityListService.independent(vo);
    }
}
